<style>
.con {
  margin-top: 50px;
  /* display: flex;
    justify-content: space-between;
    align-items: center; */
}
</style>
<template>
  <div class="con">
    <el-form
      ref="form"
      :model="proNewProcure"
      :rules="rules"
      label-width="150px"
    >
      <el-col :span="8">
        <el-form-item label="货号" prop="productNo">
          <el-input
            v-model="proTemplate.productNo"
            placeholder="请输入货号"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="图片" prop="picUrl">
          <image-preview
            :src="proTemplateSecondry.picUrl"
            :width="80"
            :height="80"
          />
        </el-form-item>
        <!-- //-----------新增或修改方法里上传图片 v-show="scope.row.picUrl"-->
        <!-- <el-form-item label="图片地址" prop="picUrl">
                <el-upload :action="imgUpload.url" :headers="imgUpload.headers" list-type="picture-card" :limit="limit"
                    :on-exceed="handleExceed" :on-success="handlePictureSuccessPic" :before-upload="beforeAvatarUpload"
                                                                                                                                                                                                                            :on-preview="handlePictureCardPreview" :file-list="files">
                                                                                                                                                                                                                                                                <i class="el-icon-plus"></i>
                                                                                                                                                                                                                                                            </el-upload>
                                                                                                                                                                                                                                                            <el-dialog :visible.sync="dialogVisible">
                                                                                                                                                                                                                                                        <img width="100%" v-if="picUrl" :src="picUrl" alt="">
                                                                                                                                                                                                                                                    </el-dialog>
                                                                                                                                                                                                                                                </el-form-item> -->
        <el-form-item label="颜色属性" prop="colorProperty">
          <el-input
            v-model="proTemplateSecondry.colorProperty"
            placeholder="请输入颜色属性"
            :disabled="true"
          />
        </el-form-item>
        <!-- <el-form-item label="尺寸" prop="proTemplateSizeList">
                <el-checkbox-group v-model="checked" @change="test">
                    <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id" :disabled="true">{{
                        item.label }}
                        </el-checkbox>
                    </el-checkbox-group>

                </el-form-item>
                <el-form-item label="logo图片" prop="logoUrl">
                    <template #default="scope">
                        <el-popover effect="light" trigger="hover" placement="right" width="auto">
                            <template #default>
                                <img :src='pjtUrl + proTemplateSecondry.logoUrl'
                                    style="object-fit: contain;width: 300px;height: 200px;">
                            </template>
                                    <template #reference>
                                        <img :src='pjtUrl + proTemplateSecondry.logoUrl'
                                style="object-fit: contain;width: 100px; height: 100px" />
                        </template>
                    </el-popover>
                </template>
            </el-form-item> -->
        <!-- //-----------新增或修改方法里上传图片 v-show="scope.row.logoUrl"-->
        <!-- <el-form-item label="logo图片" prop="logoUrl">
                <el-upload :action="imgUpload.url" :headers="imgUpload.headers" list-type="picture-card" :limit="limit"
                    :on-exceed="handleExceed" :on-success="handlePictureSuccess" :before-upload="beforeAvatarUpload"
                                                                                                                                                                                                                            :on-preview="handlePictureCardPreview" :file-list="fileList">
                                                                                                                                                                                                                                                        <i class="el-icon-plus"></i>
                                                                                                                                                                                                                                                    </el-upload>
                                                                                                                                                                                                                                                            <el-dialog :visible.sync="dialogVisible">
                                                                                                                                                                                                                                                                <img width="100%" v-if="imageUrl" :src="imageUrl" alt="">
                                                                                                                                                                                                                                                            </el-dialog>
                                                                                                                                                                                                                                                        </el-form-item> -->
        <!-- <el-form-item label="生产厂家代码" prop="manufacturerCode">
                            <el-input v-model="proTemplate.manufacturerCode" placeholder="请输入生产厂家代码" :disabled="true" />
                        </el-form-item> -->
        <el-form-item
          label="设计-特别呈现卖点"
          prop="designSpecialPresentation"
        >
          <el-input
            v-model="proTemplate.designSpecialPresentation"
            placeholder="请输入设计-特别呈现卖点"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="设计-运动强度" prop="designExerciseIntensity">
          <el-input
            v-model="proTemplate.designExerciseIntensity"
            placeholder="请输入设计-运动强度"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="设计-面料功能" prop="designFabricFunction">
          <el-input
            v-model="proTemplate.designFabricFunction"
            placeholder="请输入设计-面料功能"
            :disabled="true"
          />
        </el-form-item>
      </el-col>
      <el-col :span="2"> <el-form-item></el-form-item></el-col>
      <el-col :span="8">
        <!-- //-----------新增或修改方法里上传图片 -->
        <el-form-item label="客服-图片" prop="customerPicture">
          <el-upload
            :action="imgUpload.url"
            :headers="imgUpload.headers"
            list-type="picture-card"
            :limit="limit"
            :on-exceed="handleExceed"
            :on-success="handlePictureSuccessCustomer"
            :before-upload="beforeAvatarUpload"
            :on-preview="handlePictureCardPreview"
            :file-list="fileLi"
            :on-change="changeFile1"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" v-if="imageUrl" :src="imageUrl" alt="" />
          </el-dialog>
        </el-form-item>
        <!-- <el-form-item label="客服-图片" prop="customerPicture">
                                                                                                                                                                        <el-input v-model="proNewRelation.customerPicture" placeholder="后面完善" />
                                                                                                                                                                    </el-form-item> -->
        <el-form-item label="客服-卖点提炼" prop="customerSellingPoint">
          <el-input
            v-model="proTemplate.customerSellingPoint"
            placeholder="请输入客服-卖点提炼"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <el-form-item label="客服-产品话术" prop="customerProductTalking">
          <el-input
            v-model="proTemplate.customerProductTalking"
            placeholder="请输入客服-产品话术"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <el-form-item
          label="客服-强度/适合人群"
          prop="customerStrengthSuitable"
        >
          <el-input
            v-model="proTemplate.customerStrengthSuitable"
            placeholder="请输入客服-强度/适合人群"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <div class="footer" style="text-align: center">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel" style="margin-left: 30px"
            >返回上一级</el-button
          >
        </div>
      </el-col>
    </el-form>
  </div>
</template>
<script>
import {
  listRelation,
  getRelation,
  delRelation,
  addRelation,
  updateRelation,
} from "@/api/product/relation";
import {
  listPurchase,
  getPurchase,
  delPurchase,
  addPurchase,
  updatePurchase,
} from "@/api/edit/purchase";
//--------导入token
import { getToken } from "@/utils/auth";
export default {
  name: "Purchase",
  data() {
    return {
      customerPicture: "",
      proNewRelation: {},
      //图片相对路径
      picurl: "",
      //图片地址
      picUrl: "",
      //logo相对路径
      logoUrl: "",
      pjtUrl: process.env.VUE_APP_BASE_API,
      // 图片数量限制
      limit: 2,
      //页面上存的暂时图片地址List
      files: [{ url: "" }],
      //页面上存的暂时logo图片地址List
      fileList: [{ url: "" }],
      //页面上存的暂时客服图片地址List
      // fileLi: [{ url: "" }],
      fileLi: [],
      //图片地址
      imageUrl: "",
      dialogVisible: false,
      imgUpload: {
        // 设置上传的请求头部
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        // 图片上传的方法地址:
        url: process.env.VUE_APP_BASE_API + "/common/upload",
      },
      checked: [],
      checkList: [
        { id: 1, label: "XS" },
        { id: 2, label: "S" },
        { id: 3, label: "M" },
        { id: 4, label: "L" },
        { id: 5, label: "XL" },
        { id: 6, label: "XXL" },
        { id: 7, label: "均码" },
        // { id: 0002, label: '汽车' },
        // { id: 0002, label: '火车' },
      ],
      // pjtUrl: process.env.VUE_APP_BASE_API,
      proTemplateSecondry: {}, //二级
      proTemplate: {}, //一级
      // 表单参数
      form: {
        // proNewProcure:
      },
      proNewProcure: {},
      // 表单校验
      rules: {},
    };
  },

  created() {
    // this.getList();
    this.handleUpdate();
  },
  methods: {
    test() {
      this.$modal.msgError("禁止修改尺寸");
    },
    /** 查询产品采购信息列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then((response) => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.$router.go(-1);
      return;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.files = undefined; //加的是这一行
      this.fileList = undefined; //加的是这一行
      this.fileLi = undefined; //加的是这一行
      this.proNewProcure = {
        id: null,
        proNewId: null,
        orderNum: null,
        orderTime: null,
        fabricCycle: null,
        workshopCycle: null,
        shippingTime: null,
        fabricComposition: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const id = row.id || this.ids
      const id = this.$route.query.id;
      if (!id) {
        this.$modal.msgError("请点击左侧菜单-上新管理进入");
        return;
      }
      getRelation(id).then((response) => {
        this.proNewRelation = response.data.proNewRelation;
        if (response.data.proNewProcure) {
          this.proNewProcure = response.data.proNewProcure;
        }

        //二级
        this.proTemplateSecondry = response.data.proTemplateSecondry;
        //一级
        this.proTemplate = response.data.proTemplate;
        this.form = response.data;
        console.log(JSON.stringify(this.form));
        this.open = true;
        this.title = "修改产品采购信息";
        //图片回显
        this.files = [
          {
            url:
              process.env.VUE_APP_BASE_API +
              response.data.proTemplateSecondry.picUrl,
          },
        ]; //图片加的是这一行
        this.fileList = [
          {
            url:
              process.env.VUE_APP_BASE_API +
              response.data.proTemplateSecondry.logoUrl,
          },
        ]; //logo图片加的是这一行
        this.fileLi = response.data.proTemplate.customerPicture
          ? [
              {
                url:
                  process.env.VUE_APP_BASE_API +
                  response.data.proTemplate.customerPicture,
              },
            ]
          : []; //logo图片加的是这一行
        //尺寸回显

        if (
          response.data.proNewVO.proNewSizeList &&
          response.data.proNewVO.proNewSizeList.length > 0
        ) {
          let val = response.data.proNewVO.proNewSizeList.map((item) => {
            return item.size;
          });
          console.log(JSON.stringify(val));
          let m = [];
          for (let i = 0; i < val.length; i++) {
            // const element = array[i];
            this.checkList.forEach((item) => {
              if (val[i] == item.label) {
                m.push(item.id);
              }
            });
          }

          console.log(JSON.stringify(m));
          this.checked = m;
          // this.$set(this.form, 'checked', m)
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.proTemplate.customerPicture = this.customerPicture;
      this.form.proTemplateSecondry.picUrl = this.picurl;
      this.form.proTemplateSecondry.logoUrl = this.logoUrl; // 注：重要(用于添加到数据库)，
      this.form.proNewProcure = this.proNewProcure;
      console.log(JSON.stringify(this.form));
      updateRelation(this.form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        console.log(JSON.stringify(response));
        this.handleUpdate();
      });
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
          } else {
            // addPurchase(this.form).then(response => {
            //     this.$modal.msgSuccess("新增成功");
            //     this.open = false;
            //     this.getList();
            // });
          }
        }
      });
    },
    //图片上传前的相关判断
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type == "image/png";
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 5MB!");
      }
      return isJPG && isLt2M;
    },
    //图片预览
    handlePictureCardPreview(file) {
      this.picUrl = file.url;
      this.imageUrl = file.url;
      this.customerPicture = file.url;
      this.dialogVisible = true;
    },
    handlePictureSuccessPic(res, file) {
      //设置图片访问路径 （url 后台传过来的的上传地址）
      this.picUrl = file.response.url;
      // const url = file.response.fileName;
      this.picurl = file.response.fileName;
    },
    //图片上传成功后的回调
    handlePictureSuccess(res, file) {
      //设置图片访问路径 （url 后台传过来的的上传地址）
      this.imageUrl = file.response.url;
      // const url = file.response.fileName;
      this.logoUrl = file.response.fileName;
      // console.log(JSON.stringify(url));
    },
    //图片上传成功后的回调
    handlePictureSuccessCustomer(res, file) {
      //设置图片访问路径 （url 后台传过来的的上传地址）
      // this.customerPicture = file.response.url;
      // const url = file.response.fileName;
      this.customerPicture = file.response.fileName;
      console.log(JSON.stringify(this.customerPicture));
    },

    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传链接LOGO图片数量不能超过 ${this.limit} 个!`);
    },
    changeFile1(file, fileList) {
      if (fileList.length > 1) {
        this.fileLi = fileList.slice(-1);

        // this.fileList = [fileList[fileList.length - 1]]//这一步，是 展示最后一次选择文件
      }
      // this.fileSpecial = fileList;
    },
  },
};
</script>